import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;


public class ReceptionOrdre extends Thread {
	UDP u ;
	HashMap<Integer, Bus> l;
	
	public ReceptionOrdre(HashMap<Integer, Bus> lb) throws SocketException{
		l = lb;
		u = new UDP(4001, 1024);
	}
	
	public int numeroBus(String s){
		return Integer.parseInt(s.split("\n")[0].split(":")[1]);
	}
	
	public int vitesse(String s){
		if (s.contains("vitesse")){
			System.out.println(s);
			return Integer.parseInt(s.split("\n")[1].split(":")[1]);
		}
		else return 0;
			
	}
	
	public String destination(String s){
		if (s.contains("destination")){
			if (s.contains("vitesse")){ 
				//System.out.println(s.split("\n")[2].split(":")[1]);
				return s.split("\n")[2].split(":")[1];
			}
			else{
				//System.out.println(s.split("\n")[1].split(":")[1]);
				return s.split("\n")[1].split(":")[1];
			}
		}	
		else return null;
	}

	public Bus getBus(int num){
		return l.get(num);
	}
	
	
	
	public void run(){
		String s;
		try{
			while (true){
				s = u.recevoir();
				//System.out.println("ordre recu : "+s);
				getBus(numeroBus(s)).modifierVitesse(vitesse(s));
				getBus(numeroBus(s)).modifieDestination(destination(s));
				//System.out.println("Ordre applique :\n"+getBus(numeroBus(s)).toString());
			}
		}catch (IOException e){
			e.printStackTrace();
		}
		
	}

}
